<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('ustcStudentInfoFamily.fields.accountId')" prop="accountId">
        <el-input
          v-model="formData.accountId"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.accountId')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.sort')" prop="sort">
        <el-input
          v-model="formData.sort"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.sort')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.name')" prop="name">
        <el-input
          v-model="formData.name"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.name')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.gender')" prop="gender">
        <el-input
          v-model="formData.gender"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.gender')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.relationship')" prop="relationship">
        <el-input
          v-model="formData.relationship"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.relationship')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.unitName')" prop="unitName">
        <el-input
          v-model="formData.unitName"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.unitName')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.career')" prop="career">
        <el-input
          v-model="formData.career"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.career')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.position')" prop="position">
        <el-input
          v-model="formData.position"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.position')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.telephone')" prop="telephone">
        <el-input
          v-model="formData.telephone"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.telephone')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.address')" prop="address">
        <el-input
          v-model="formData.address"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.address')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoFamily.fields.politicalStatus')"
        prop="politicalStatus"
      >
        <el-radio-group v-model="formData.politicalStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.identityType')" prop="identityType">
        <el-select
          v-model="formData.identityType"
          :placeholder="
            t('common.selectTextPlaceholder') + t('ustcStudentInfoFamily.fields.identityType')
          "
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.identityNumber')" prop="identityNumber">
        <el-input
          v-model="formData.identityNumber"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.identityNumber')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoFamily.fields.guardian')" prop="guardian">
        <el-radio-group v-model="formData.guardian">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { UstcStudentInfoFamilyApi, UstcStudentInfoFamilyVO } from '@/api/core/ustcstudentinfofamily'
import { FormRules } from 'element-plus'

/** 学生家庭成员信息 表单 */
defineOptions({ name: 'StudentInfoFamilyForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  accountId: undefined,
  sort: undefined,
  name: undefined,
  gender: undefined,
  relationship: undefined,
  unitName: undefined,
  career: undefined,
  position: undefined,
  telephone: undefined,
  address: undefined,
  politicalStatus: undefined,
  identityType: undefined,
  identityNumber: undefined,
  guardian: undefined
})
const formRules = reactive<FormRules>({
  id: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.id'),
      trigger: 'blur'
    }
  ],
  accountId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.accountId'),
      trigger: 'blur'
    }
  ],
  sort: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.sort'),
      trigger: 'blur'
    }
  ],
  name: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.name'),
      trigger: 'blur'
    }
  ],
  gender: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.gender'),
      trigger: 'blur'
    }
  ],
  relationship: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.relationship'),
      trigger: 'blur'
    }
  ],
  unitName: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.unitName'),
      trigger: 'blur'
    }
  ],
  career: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.career'),
      trigger: 'blur'
    }
  ],
  position: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.position'),
      trigger: 'blur'
    }
  ],
  telephone: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.telephone'),
      trigger: 'blur'
    }
  ],
  address: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.address'),
      trigger: 'blur'
    }
  ],
  politicalStatus: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.politicalStatus'),
      trigger: 'blur'
    }
  ],
  identityType: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.identityType'),
      trigger: 'change'
    }
  ],
  identityNumber: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.identityNumber'),
      trigger: 'blur'
    }
  ],
  guardian: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.guardian'),
      trigger: 'blur'
    }
  ],
  creator: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.creator'),
      trigger: 'blur'
    }
  ],
  createTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.createTime'),
      trigger: 'blur'
    }
  ],
  updater: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.updater'),
      trigger: 'blur'
    }
  ],
  updateTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.updateTime'),
      trigger: 'blur'
    }
  ],
  deleted: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.deleted'),
      trigger: 'blur'
    }
  ],
  tenantId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoFamily.fields.tenantId'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await UstcStudentInfoFamilyApi.getUstcStudentInfoFamily(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as UstcStudentInfoFamilyVO
    if (formType.value === 'create') {
      await UstcStudentInfoFamilyApi.createUstcStudentInfoFamily(data)
      message.success(t('common.createSuccess'))
    } else {
      await UstcStudentInfoFamilyApi.updateUstcStudentInfoFamily(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    accountId: undefined,
    sort: undefined,
    name: undefined,
    gender: undefined,
    relationship: undefined,
    unitName: undefined,
    career: undefined,
    position: undefined,
    telephone: undefined,
    address: undefined,
    politicalStatus: undefined,
    identityType: undefined,
    identityNumber: undefined,
    guardian: undefined
  }
  formRef.value?.resetFields()
}
</script>
